# Maintainer: Daniel Bermond <dbermond@archlinux.org>
# Maintainer: Bruno Pagani <archange@archlinux.org>
# Maintainer: Torsten Keßler <tpkessler@archlinux.org>

pkgname=intel-graphics-compiler
pkgver=1.0.14828.8
_llvmmaj=14
_llvmver="${_llvmmaj}.0.5"
_vciver=0.13.0
_spirv_llvm_commit=23f398bf369093b1fd67459db8071ffcc6b92658
_opencl_clang_commit=cf95b338d14685e4f3402ab1828bef31d48f1fd6
pkgrel=1
epoch=1
pkgdesc='Intel Graphics Compiler for OpenCL'
arch=('loong64' 'x86_64')
url='https://github.com/intel/intel-graphics-compiler/'
license=('MIT' 'custom')
depends=('gcc-libs' 'zlib' 'spirv-tools')
makedepends=('git' 'cmake' 'ninja' 'python' 'spirv-headers')
provides=("intel-opencl-clang=${_llvmmaj}.0.0")
conflicts=('intel-opencl-clang')
replaces=('intel-opencl-clang')
options=('!emptydirs' '!lto')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/intel/intel-graphics-compiler/archive/refs/tags/igc-${pkgver}.tar.gz"
        "git+https://github.com/intel/vc-intrinsics.git#tag=v${_vciver}"
        "git+https://github.com/KhronosGroup/SPIRV-LLVM-Translator.git#commit=${_spirv_llvm_commit}"
        "git+https://github.com/intel/opencl-clang.git#commit=${_opencl_clang_commit}"
        "git+https://github.com/llvm/llvm-project.git#tag=llvmorg-${_llvmver}")
sha256sums=('c62e4d68060a4527fd5932fc8dba496fb817d86293073024a90def20801fbf73'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP')

prepare() {
    ln -s "${srcdir}/SPIRV-LLVM-Translator"  "${srcdir}/llvm-project/llvm/projects/llvm-spirv"
    ln -s "${srcdir}/opencl-clang" "${srcdir}/llvm-project/llvm/projects/opencl-clang"
}

build() {
    # Prevent IGC to load LLVM 15 symbols
    CFLAGS+=" -fno-semantic-interposition"
    CXXFLAGS+=" -fno-semantic-interposition"
    LDFLAGS+=" -Wl,-Bsymbolic"

    CXXFLAGS+=" -I ${srcdir}/SPIRV-LLVM-Translator/include"

    EMAIL='builduser@archlinux.org' \
    cmake -B build -S "${pkgname}-igc-${pkgver}" \
        -G Ninja \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_INSTALL_LIBDIR=lib \
        -DIGC_OPTION__ARCHITECTURE_TARGET=Linux64 \
        -DIGC_OPTION__SPIRV_TOOLS_MODE=Prebuilds \
        -DIGC_OPTION__USE_PREINSTALLED_SPIRV_HEADERS=ON \
        -DIGC_OPTION__CLANG_MODE=Source \
        -DIGC_OPTION__LLD_MODE=Source \
        -DIGC_OPTION__LLVM_PREFERRED_VERSION="${_llvmver}" \
        -DIGC_OPTION__LLVM_MODE=Source \
        -DIGC_OPTION__LINK_KHRONOS_SPIRV_TRANSLATOR=OFF \
        -DIGC_OPTION__VC_INTRINSICS_MODE=Source \
        -DCCLANG_FROM_SYSTEM=OFF \
        -DINSTALL_GENX_IR=ON \
        -Wno-dev
    cmake --build build
}

package() {
    DESTDIR="$pkgdir" cmake --install build
    install -D -m644 "${pkgname}-igc-${pkgver}"/LICENSE.md -t "${pkgdir}/usr/share/licenses/${pkgname}"
    mv "${pkgdir}/usr/include"/opencl-c{,-base}.h "${pkgdir}/usr/include/igc"
    mv "${pkgdir}/usr/lib/igc/NOTICES.txt" "${pkgdir}/usr/share/licenses/${pkgname}"
    rm "${pkgdir}/usr/bin/lld"
    
    # additional files for opencl-clang
    install -D -m644 opencl-clang/common_clang.h -t "${pkgdir}/usr/include/cclang"
    install -D -m644 opencl-clang/LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE-opencl-clang"
    ln -s "libopencl-clang.so.${_llvmmaj}" "${pkgdir}/usr/lib/libopencl-clang.so"
}
